﻿using JESAI.Core.Logging;
using JESAI.Core.Modularity;
using NLog.Extensions.Logging;
using System.ComponentModel;
using JESAI.MQTT.Client.ServiceCollections;
using MQTTnet.AspNetCore;
using JESAI.MQTT.Server.ServiceCollections;
using JESAI.MQTT.Server.Hosts;

namespace JESAI.Platform.WebApi.Modules
{
    /// <summary>
    /// MQTT模块
    /// </summary>
    [Description("MQTT模块")]
    [DependsOnModule(typeof(AspNetCoreModule))]
    public class MQTTModule : JESAIModule
    {
        /// <summary>
        /// 模块级别。级别越小越先启动
        /// </summary>
        public override ModuleLevel Level => ModuleLevel.Framework;

        /// <summary>
        /// 模块启动顺序。模块启动的顺序先按级别启动，同一级别内部再按此顺序启动，
        /// 级别默认为0，表示无依赖，需要在同级别有依赖顺序的时候，再重写为>0的顺序值
        /// </summary>
        public override int Order => 8;

        /// <summary>
        /// 添加服务。将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">服务集合</param>
        public override IServiceCollection AddServices(IServiceCollection services)
        {
            //services.AddJESAINLog();
            var configuration = services.GetConfiguration();
            //services.AddJESAIMQTTServer(configuration);
            //services.AddJESAIMQTTClient(configuration);
            services.AddHostedService<MqttHostService>();
            services.AddJESAIMQTTClient(configuration);
            return services;
        }


        public override void UseModule(IApplicationBuilder app)
        {
            //app.UseJESAIMqttServer();

            //app.UseEndpoints((endPoint) => {
            //    endPoint.MapMqttServer("/mqtt");
            //});
        }
    }
}
